package cn.wolfcode.shop;

import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.domain.PropertyValue;
import cn.wolfcode.shop.service.IPropertyValueService;
import cn.wolfcode.shop.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class PropertyValueController {
    @Reference
    private IPropertyValueService propertyValueService;

    @RequestMapping("/propertyValue/get/{propertyId}")
    public String propertyValueList(@PathVariable("propertyId") Long propertyId, Model model){
        model.addAttribute("propertyValueList", propertyValueService.selectByPId(propertyId));
        return "property/property_value_list";
    }

    @RequestMapping("/propertyValue/save")
    @ResponseBody
    public JSONResultVo saveOrUpdate(@RequestBody List<PropertyValue> propertyValues){
        JSONResultVo vo = new JSONResultVo();
        try {
            propertyValueService.saveOrUpdate(propertyValues);
        } catch (Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }

    @RequestMapping("/propertyValue/delete/{id}")
    @ResponseBody
    public JSONResultVo delete(@PathVariable("id") Long id){
        JSONResultVo vo = new JSONResultVo();
        try {
            propertyValueService.delete(id);
        } catch (Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }
}
